package nobnak.study.opensles;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import nobnak.study.opensles.Recorder.RecorderListener;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class StudyOpenSLESActivity extends Activity {
	private File mp3file;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		if (!Recorder.createEngine())
			finish();
		if (!Recorder.createAudioRecorder())
			finish();
		
		final MediaPlayer player = new MediaPlayer();

		final Button startRecordingButton = (Button) findViewById(R.id.startRecordingButton);
		final Button playbackButton = (Button) findViewById(R.id.startPlaybackButton);
		
		startRecordingButton.setOnClickListener(new OnClickListener() {
			private boolean recording = false;

			@Override
			public void onClick(View v) {
				if (recording) {
					Log.d(getPackageName(), String.format("Stop recording"));
					if (Recorder.stopRecording()) {
						recording = false;
						startRecordingButton
								.setText(getString(R.string.recordingButton_init));
					}
					return;
				}

				try {
					if (Recorder.startRecording()) {
						mp3file = File
								.createTempFile("debug", "mp3", getCacheDir());
						final OutputStream streamout = new FileOutputStream(mp3file);

						Recorder.setListener(new RecorderListener() {
							@Override
							public void read(byte[] pcm) {
								Log.d(getPackageName(),
										String.format("pcm length=%d", pcm.length));
								try {
									streamout.write(pcm);
								} catch (IOException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void end() {
								Log.d(getPackageName(), "End stream");
								try {
									streamout.flush();
									streamout.close();
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						});

						Log.d(getPackageName(), "Start recording");
						recording = true;
						startRecordingButton
								.setText(getString(R.string.recordingButton_recording));
					}
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});
		
		playbackButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					player.reset();
					player.setAudioStreamType(AudioManager.STREAM_MUSIC);
					FileInputStream in = new FileInputStream(mp3file);
					player.setDataSource(in.getFD());
					player.prepare();
					player.start();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		Recorder.shutdown();
		super.onDestroy();
	}

}